package guanqu.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import guanqu.beans.IrrBAWaterGate;
import guanqu.beans.IrrBRStadia;
import manager.PersistentManager;

/**
 * 获取水闸基本信息
 * Servlet implementation class GetWaterGate
 */
//@WebServlet("/GetWaterGate")
@Deprecated
public class GetWaterGate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetWaterGate() {
        super();
        
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		
		Download download = new Download();
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		Session session = sf.getCurrentSession();
		
		Transaction tx = session.beginTransaction();
		try
		{
			
			Query q = session.createQuery("from IrrBAWaterGate");
			
			List<IrrBAWaterGate> irrBAWaterGates = q.list();
			for(IrrBAWaterGate g:irrBAWaterGates)
			{
				Query qtemp = session.createQuery("from IrrBRStadia stadia where stadia.locCode = ?");
				qtemp.setParameter(0, g.getWatergateCode());
				List<IrrBRStadia> stadias = qtemp.list();
				if (stadias != null && !stadias.isEmpty()) {
					g.setStadiaCode(stadias.get(0).getStadiaCode());
				}
			}
			
			download.setIrrBAWaterGates(irrBAWaterGates);
			tx.commit();
			download.setStatus(Download.SUCCESS);
		}catch(Exception e)
		{
			download.setStatus(Download.FAILED);
			tx.rollback();
			e.printStackTrace();
		}finally {
//			session.close();
		}
		
		response.setCharacterEncoding("UTF-8");
		System.out.println(JSON.toJSONString(download));
		response.getWriter().append(JSON.toJSONString(download));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	public static class Download
	{
		public static final int SUCCESS = 1;
		public static final int FAILED = 2;
		int status;
		
		List<IrrBAWaterGate> irrBAWaterGates = new ArrayList<>();

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}

		public List<IrrBAWaterGate> getIrrBAWaterGates() {
			return irrBAWaterGates;
		}

		public void setIrrBAWaterGates(List<IrrBAWaterGate> irrBAWaterGates) {
			this.irrBAWaterGates = irrBAWaterGates;
		}
		
		
	}
	
}
